﻿using yao.Common;
using yao.Entity;
using yao.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Titan;

namespace yao.website.Controllers
{
    /// <summary>
    /// 库存查询
    /// </summary>
    public class StoreProductController:BaseController
    {
        public ActionResult Index(int? p)
        {
            int pageIndex = p == null ? 1 : (int)p;
            ViewBag.PageIndex = pageIndex;

            using (IDbSession cn = OpenSession())
            {
                QueryExpression q = new QueryExpression();
                q.EntityType = typeof(Product);
                q.Selects.Add(Product_.ProductId);
                q.Selects.Add(Product_.Image60);
                q.Selects.Add(Product_.ProductName);//商品名称
                q.Selects.Add(Product_.ProductNo);//商品编号
                q.Wheres.Add(Product_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                q.PageSize = 20;
                q.PageIndex = pageIndex;
                q.ReturnMatchedCount = true;
                Products products = new Products();
                products.TotalCount = cn.SelectCollection(products.Items, q);

                for (int i = 0; i < products.Items.Count; i++) 
                {
                    QueryExpression query = new QueryExpression();
                    query.EntityType = typeof(StoreProduct);
                    query.Selects.Add(StoreProduct_.StoreId);
                    query.Selects.Add(StoreProduct_.Store.StoreName);
                    query.Selects.Add(StoreProduct_.QualityStyleId);
                    query.Selects.Add(StoreProduct_.LotNo);
                    query.Selects.Add(StoreProduct_.UseableQuantity);
                    query.Selects.Add(StoreProduct_.LockQuantity);
                    query.Selects.Add(StoreProduct_.Quantity);
                    query.Wheres.Add(StoreProduct_.ProductId.TEqual(products.Items[i].ProductId));
                    StoreProducts storeProducts = new StoreProducts();
                    cn.SelectCollection(storeProducts.Items, query);
                    products.Items[i].StoreProducts = storeProducts;
                }
                return View(products);
            }
        }

    }
}                                                                               
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                